import {createGlobalState} from "@vueuse/core";
import {ref} from "vue";
import getUserInfoAPI, {type IUser} from "@/api/user/getUserInfoAPI";
import {SUCCESS_CODE} from "@/constants";
import {showToast} from "vant";
import useLoading from "@/hooks/useLoading";

const useUserDetail = createGlobalState(() => {
    const {toggle} = useLoading();
    const user = ref<IUser>({
        nickName: '',
        loginName: '',
        introduceSign: ''
    });
    const handleSearchUserDetail = async () => {
        toggle(true);
        try {
            const response = await getUserInfoAPI();
            if (response.code === SUCCESS_CODE) {
                user.value = response.data;
            }
        } catch (e: any) {
            showToast(e.message);
        } finally {
            toggle(false);
        }
    }
    return {
        user,
        handleSearchUserDetail
    }
})

export default useUserDetail;